package gui.beans;

import java.awt.Font;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.JTextArea;
import javax.swing.text.DefaultHighlighter;

import control.Buscador;

import gui.eventos.UpdateEvent;
import gui.eventos.UpdateListener;

class MyTextPane extends JTextArea {
	/**
	 * 
	 */
	private static final long serialVersionUID = -7754775098643116666L;

	private List<UpdateListener> listeners = null;

	private Buscador buscador = null;

	public MyTextPane() {
		super();
		
		buscador = Buscador.instanciar();
		
		setHighlighter(new DefaultHighlighter());

		addKeyListener(new java.awt.event.KeyAdapter() {
			public void keyTyped(java.awt.event.KeyEvent e) {
				cambio = true;
			}
		});

		addFocusListener(new java.awt.event.FocusAdapter() {
			public void focusLost(java.awt.event.FocusEvent e) {
				if (cambio) {
					cambio = false;
					fireUpdateEvent();
				}
			}
		});

		setLineWrap(true);
		setFont(new Font("Dialog", Font.PLAIN, 11));
		setWrapStyleWord(true);
	}	
	
	public Buscador getBuscador() {
		return buscador;
	}

	public void addUpdateListener(UpdateListener listener) {
		if (listeners == null)
			listeners = new ArrayList<UpdateListener>();

		listeners.add(listener);
	}

	private void fireUpdateEvent() {
		if (listeners == null)
			return;

		UpdateEvent e = new UpdateEvent(this, getText());
		Iterator<UpdateListener> it = listeners.iterator();
		while (it.hasNext()) {
			it.next().valueUpdated(e);
		}
	}
		
	public boolean cambio = false;
}